package com.application.model.gobierno.caba.retrieve;

import com.application.model.RetrivierWeb;
import com.application.model.gobierno.caba.database.GobCABADB;
import com.application.model.gobierno.caba.entities.GobCabaColegioItem;
import com.application.model.gobierno.caba.entities.ManagerGobCabaColegio;

public class RetrieveGobCABA extends RetrivierWeb {

	private ManagerGobCabaColegio	manager;
	private RetrieveGobCABAAviso	retrieveGobCABAAviso;
	
    public RetrieveGobCABA() {
    	manager = new ManagerGobCabaColegio();
    	retrieveGobCABAAviso = new RetrieveGobCABAAviso(manager);    	
    }

    
    
    @Override
    public void retrieve() {
        String beginPatter = "</thead";
        String endPatter = "Información suministrada por la";
        
        String url = "http://www.buenosaires.edu.ar/areas/educacion/establecimientos/index.php?menu_id=&redir=1&orden=2&pag=";
        
        for (int i = 1; i <= 25; i++) {
			retrivie(url+i, "aviso", beginPatter, endPatter,"iso-8859-1");
		}
        saveFile();
        
    }
    public void saveFile(){
    	manager.save();
    }
    @Override
    public void processContent(String data, String parentName) {
//    	if(parentName.equals("aviso")){
    		retrieveGobCABAAviso.processContent(data, parentName);
//    	}else if(parentName.equals("detalle")){
//    		retrieveGobCABADetalle.processContent(data, parentName);
//    	}
    }
    
    

	public void saveDataInDataBase() {
		try{
			this.manager.load();
			GobCABADB database = new GobCABADB(this); 
			if(!manager.getValues().isEmpty()){
				for (GobCabaColegioItem item : manager.getValues()) {
					database.save(item);
				}
				saveFile();
			}
		}catch (Exception e) {
			logger.error(e);
		}
		
	}
}
